package com.wdc.deliveryrobot.fragment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.sirui.selfstudysdk.main.SelfChassisState;
import com.sirui.selfstudysdk.mode.ChargingPosition;
import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.activity.BaseActivity;
import com.wdc.deliveryrobot.activity.DeliveryActivity;
import com.wdc.deliveryrobot.activity.SettingActivity;
import com.wdc.deliveryrobot.adapter.LocationAdapter;
import com.wdc.deliveryrobot.dialog.HintDialog;

import java.util.ArrayList;
import java.util.Arrays;

public class MainFragment extends Fragment implements View.OnClickListener {

    private SelfChassisState selfChassisState;

    private LinearLayout linearLayoutOne,linearLayoutTwo,linearLayoutThree,linearLayoutFour,linearLayoutSetting,linearLayoutReturn,linearLayoutCharge;

    private OnItemClickListener listener;

    public interface OnItemClickListener{
        void checkPag(int page);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 加载 Fragment 的布局
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        linearLayoutOne = view.findViewById(R.id.layout_one);
        linearLayoutTwo = view.findViewById(R.id.layout_two);
        linearLayoutThree = view.findViewById(R.id.layout_three);
        linearLayoutFour = view.findViewById(R.id.layout_four);
        linearLayoutSetting  = view.findViewById(R.id.layout_five);
        linearLayoutReturn = view.findViewById(R.id.return_back);
        linearLayoutCharge = view.findViewById(R.id.return_charge);

        linearLayoutOne.setOnClickListener(this);
        linearLayoutTwo.setOnClickListener(this);
        linearLayoutThree.setOnClickListener(this);
        linearLayoutFour.setOnClickListener(this);
        linearLayoutSetting.setOnClickListener(this);
        linearLayoutReturn.setOnClickListener(this);
        linearLayoutCharge.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.layout_one:
                if (listener != null)
                    listener.checkPag(1);
                break;
            case R.id.layout_two:
                if (listener != null)
                    listener.checkPag(2);
                break;
            case R.id.layout_three:
                if (listener != null)
                    listener.checkPag(3);
                break;
            case R.id.layout_four:
                if (listener != null)
                    listener.checkPag(4);
                break;
            case R.id.layout_five:
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.return_back:
                HintDialog hintDialog1 = new HintDialog(getContext());
                hintDialog1.setMessage(getString(R.string.ok_navigation));
                hintDialog1.setCancelButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        hintDialog1.dismiss();

                    }
                });
                hintDialog1.setOkButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(getContext(), "现在出发了", Toast.LENGTH_SHORT).show();

                    }
                });
                hintDialog1.show();
                break;
            case R.id.return_charge:
                HintDialog hintDialog = new HintDialog(getContext());
                hintDialog.setMessage(getString(R.string.ok_change));
                hintDialog.setCancelButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        hintDialog.dismiss();

                    }
                });
                hintDialog.setOkButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ChargingPosition chargingPosition = selfChassisState.getChargingPosition();
                        if (chargingPosition == null){
                            //说明没有设置充电点位
                            HintDialog hintDialog2 = new HintDialog(getContext());
                            hintDialog2.setMessage(getString(R.string.please_charging));
                            hintDialog2.setOkButtonClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    hintDialog2.dismiss();
                                }
                            });
                            hintDialog2.setOkButtonClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    hintDialog2.dismiss();
                                }
                            });
                            hintDialog2.show();
                        }else {
                            //已经设置了充电点位，让他去充电
                            selfChassisState.automaticCharging();
                            Log.d("thd_","机器去充电");
                        }
                    }
                });
                hintDialog.show();
                break;
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            selfChassisState = ((BaseActivity) context).selfChassisState;
        }
    }

    //设置监听
    public void setListener(OnItemClickListener listener){
        this.listener = listener;
    }
}